<?php

namespace App\Services;
use App\Models\Beat;
use App\Models\MusicCreation;
use App\Models\WxUsers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class BeatService
{
    /**
     * @param $request
     * @return array
     * @name Beat 信息
     * @user IHammer
     */
    public static function BeatInfoHandle($request){
        $beat_id = $request->get("beat_id")?:0;
        if(empty($beat_id)){
            return [];
        }
        $beat = Beat::getInfo(['id'=>$beat_id,'status'=>1]);
        if(empty($beat)){
            return [];
        }
        return $beat;
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 点击量集中设置
     * @user IHammer
     */
    public static function BeatClickHandle($request,&$errmsg){
        $type = $request->get("type")?:"";
        $id = $request->get("id")?:0;
        if(empty($type)||empty($id)){
            return false;
        }
        if(!in_array($type,["beat",'music'])){
            return false;
        }
        if($type=="beat"){
            $beat = Beat::getInfo(['id'=>$id,"status"=>1]);
            if(empty($beat)){
                $errmsg = "没有找到对应数据！";
                return false;
            }
            @Beat::where(['id' => $id])->increment('click_num',1);
            return true;
        }elseif($type=="music"){
            $beat = MusicCreation::getInfo(['id'=>$id,"status"=>1]);
            if(empty($beat)){
                $errmsg = "没有找到对应数据！";
                return false;
            }
            @MusicCreation::where(['id' => $id])->increment('click_num',1);
            return true;
        }
        return false;
    }
    /**
     * @param $request
     * @return array
     * @name Beat 列表数据
     * @user IHammer
     */
    public static function BeatListsHandle($request){
        $lists =  Beat::getListsWx($request);
        if(empty($lists["data"])){
            return [];
        }
        $store_array = array();
        foreach($lists['data'] as $list){
            $store['is_collection'] = CollectionService::isCollection($list['id'],'beat');
            $store['beat_id'] = $list['id'];
            $store['beat_title'] = $list['beat_title'];
            $store['beat_author'] = $list['beat_author'];
            $store['beat_image'] = $list['beat_image'];
            $store['beat_url'] = str_replace('https://file.ihammer.cn','',$list['beat_url']);
            $store['beat_time'] = $list['beat_time']?SystemService::changeTimeType($list['beat_time']):"未读取";
            $store['use_num'] = $list['use_num'];
            $store['click_num'] = $list['click_num'];
            $store['collection_num'] = $list['collection_num'];
            $store['created_at'] = $list['created_at'];
            $store_array[] = $store;
        }
        $lists['data'] = $store_array;
        return $lists;
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 创建操作
     * @user IHammer
     */
    public static function BeatCreateHandle($request,&$errmsg){
        /**
         * 采集数据
         */
        $beat_title = $request->get("beat_title");
        $beat_author = $request->get("beat_author");
        $beat_url = $request->get("beat_url");
        /**
         * 校验数据
         */
        $beat = Beat::getInfo(['beat_title'=>$beat_title,"status"=>1]);
        if($beat){
            $errmsg = "标题重复。";
            return false;
        }
        //时长
        $time_ = SystemService::getVideoTime($beat_url);
        if(empty($time_)){
            $errmsg = "数据错误。";
            return false;
        }
        $course_time = explode(".",$time_)[0];
        $arr_duration = explode(':', $course_time);
        $course_time = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转
        /**
         * 处理数据
         */
        $res['beat_title'] = $beat_title;
        $res['beat_author'] = $beat_author;
        $res['beat_url'] = $beat_url;
        $res['beat_time'] = $course_time;
        @Beat::create($res);
        return true;
    }

    /**
     * @param $request
     * @param $errmsg
     * @return bool
     * @name 删除操作
     * @user IHammer
     */
    public static function BeatDeleteHandle($request,&$errmsg){
        /**
         * 采集数据
         */
        $beat_id = $request->get("beat_id")?:0;
        if(empty($beat_id)){
            return false;
        }
        /**
         * 校验数据
         */
        $beat = Beat::getInfo(['id'=>$beat_id,"status"=>1]);
        if(empty($beat)){
            return false;
        }
        @Beat::where(['id'=>$beat_id])->update(["status"=>0]);
        return true;
    }
}
